package com.kunka.controller;


import com.kunka.api.entity.auth.VerifyCode;
import com.kunka.api.service.auth.CharVerifyCodeService;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 初始化controller 跳转登录页面
 */
@Api(tags = "初始化控制器")
@Controller
public class InitController {

    @Reference
    private CharVerifyCodeService charVerifyCodeService;


    @RequestMapping("/welcome")
    public String toLoginPage(){

        return "login";
    }


    /**
     * 验证码 图形验证码
     */
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response){

        try {

            VerifyCode verifyCode = charVerifyCodeService.createGraphicCode();
            //将VerifyCode绑定session
            request.getSession().setAttribute("authcode", verifyCode.getCode());
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");

            response.getOutputStream().write(verifyCode.getImgBytes());

            response.getOutputStream().flush();

        } catch (IOException e) {

        }

    }




}
